
use "W:\original\u1067_al3_rpk.dta", clear

// create year variable 
gen year_event=substr(taika, -4, .)
destring year_event, replace
ren re_shnro shnro 
keep shnro year_event
g incident = 1 
drop if missing(shnro)
collapse (sum) incident, by(shnro year_event)

sort shnro year_event 
bys shnro : gen cum_crimes = sum(incident)
g sphnro = shnro // for merging later 

save "W:\intimate\data\cumul_crime_all", replace


// only violent
use "W:\original\u1067_al3_rpk.dta", clear
rename rkdi	crimecode6

gen pot_domestic=0
replace pot_domestic=1 if crimecode6=="200101" |  crimecode6=="200104" | crimecode6== "2001A3" | crimecode6== "2001A4" | crimecode6== "200201" | crimecode6== "200202" | crimecode6== "200301" | crimecode6== "200303" | crimecode6== "200401" | crimecode6== "200402" | crimecode6== "200501" | crimecode6== "200503" | crimecode6== "2005A1" | crimecode6== "200601" | crimecode6== "200603" | crimecode6== "200701" | crimecode6== "200702" | crimecode6== "200801" | crimecode6== "200804" | crimecode6== "2008A1" | crimecode6== "2008A3" | crimecode6== "2008B1" | crimecode6== "2008B3" | crimecode6== "2008C1" | crimecode6== "2008C2" | crimecode6== "2507A1" | crimecode6==  "250801" | crimecode6==  "250301" | crimecode6==  "2503A1" | crimecode6==  "2503A3"| crimecode6==  "250101"  | crimecode6==  "250201" | crimecode6==  "250303" | crimecode6==  "250701" | crimecode6==   "310101" | crimecode6==  "310102" | crimecode6==  "310301" | crimecode6==  "310302"| crimecode6==  "210101" | crimecode6== "210102" | crimecode6== "210201" | crimecode6==  "210202" | crimecode6== "210301" | crimecode6== "210302" | crimecode6== "210401" | crimecode6== "210402" | crimecode6==  "210501" | crimecode6== "210502" | crimecode6== "210601" | crimecode6== "210602" | crimecode6== "2106A1" | crimecode6== "210701" | crimecode6== "210801" | crimecode6== "210901" | crimecode6== "211001" | crimecode6== "211101"| crimecode6== "211201" | crimecode6== "211301" | crimecode6==  "211401" | crimecode6== "211501" | crimecode6=="2503B1" | crimecode6=="2503B2" | crimecode6=="2503C1" | crimecode6=="470301" | crimecode6=="4703A1" | crimecode6=="111101"
keep if pot_domestic==1

// create year variable 
gen year_event=substr(taika, -4, .)
destring year_event, replace
g incident = 1 

ren re_shnro shnro 
drop if missing(shnro)

g mm_crime = (re_sukup == "1" & ao_sukup == "1")
g mf_crime = (re_sukup == "1" & ao_sukup == "2")
g fm_crime = (re_sukup == "2" & ao_sukup == "1")
g ff_crime = (re_sukup == "2" & ao_sukup == "2")

sort shnro year_event 
foreach thing in mm mf fm ff {
	g tmp = year_event*(`thing'_crime==1) + 3000*(`thing'_crime==0)
	bys shnro : egen min_yr_`thing' = min(tmp)
	drop tmp
}

preserve
collapse (sum) incident, by(shnro year_event)

sort shnro year_event 
bys shnro : gen cum_crimes = sum(incident)
g sphnro = shnro // for merging later 

save "W:\intimate\data\cumul_crime_violent", replace

restore 

preserve
keep if mf_crime==1 
collapse (sum) incident, by(shnro year_event)

sort shnro year_event 
bys shnro : gen cum_crimes = sum(incident)
g sphnro = shnro // for merging later 

save "W:\intimate\data\cumul_crime_violent_mf", replace
restore 


preserve 
gen assault=(crimecode6=="210501") 
gen petty_assault=(crimecode6=="210701")
gen menace=(crimecode6=="250701")

collapse (sum) assault (sum) petty_assault (sum) menace, by(shnro year_event)

sort shnro year_event 
bys shnro : gen cum_assault = sum(assault)
bys shnro : gen cum_petty_assault = sum(petty_assault)
bys shnro : gen cum_menace = sum(menace)
g sphnro = shnro // for merging later 

save "W:\intimate\data\cumul_crime_violent_types", replace

restore 



preserve 
keep if mf_crime==1
gen assault=(crimecode6=="210501") 
gen petty_assault=(crimecode6=="210701")
gen menace=(crimecode6=="250701")

collapse (sum) assault (sum) petty_assault (sum) menace, by(shnro year_event)

sort shnro year_event 
bys shnro : gen cum_assault = sum(assault)
bys shnro : gen cum_petty_assault = sum(petty_assault)
bys shnro : gen cum_menace = sum(menace)
g sphnro = shnro // for merging later 

save "W:\intimate\data\cumul_crime_violent_types_mf", replace

restore 



preserve 

collapse (max) *_crime (mean) min_yr*, by(shnro)
save "W:\intimate\data\violence_gender_indicator", replace

restore



